package ProvaView;

import Media.MediaController;
import Prova.HTMLGenerator;
import Prova.Prova;
import Prova.QuestaoProva;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;

public class TelaProva extends javax.swing.JFrame
{
    private List<QuestaoProva> questoesProva = new ArrayList<QuestaoProva>();

    public List<QuestaoProva> getQuestoesProva()
    {
        return questoesProva;
    }

    public void setQuestoesProva(List<QuestaoProva> questoesProva)
    {
        this.questoesProva = questoesProva;
    }

    public TelaProva(List<QuestaoProva> questoesProva)
    {
        initComponents();
        TelaQuestaoProva.posicoes = null;
        this.questoesProva = questoesProva;
        
        populaListaQuestoesProva();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lbProva = new javax.swing.JLabel();
        lbQuestoes = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        lbxQuestoes = new javax.swing.JList();
        btnImprimriProva = new javax.swing.JButton();
        btnImprimirGabarito = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        lbProva.setText("Prova");

        lbQuestoes.setText("Questões:");

        lbxQuestoes.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        lbxQuestoes.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lbxQuestoesMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(lbxQuestoes);

        btnImprimriProva.setText("Imprimir Prova");
        btnImprimriProva.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnImprimriProvaActionPerformed(evt);
            }
        });

        btnImprimirGabarito.setText("Imprimir Gabarito");
        btnImprimirGabarito.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnImprimirGabaritoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(174, 174, 174)
                        .addComponent(lbProva))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(lbQuestoes)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(btnImprimriProva)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(btnImprimirGabarito))
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 279, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(30, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lbProva)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lbQuestoes)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 178, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(32, 32, 32)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnImprimirGabarito, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnImprimriProva))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnImprimriProvaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnImprimriProvaActionPerformed

            Prova p = new Prova();
            p.setQuestoesProva(questoesProva);

        try
        {
            if(p.validaPontuacaoTotalProva(questoesProva))
                if(p.verificaPosicaoQuestoesProva(questoesProva))
                {
                    HTMLGenerator html = new HTMLGenerator();
                    if(html.imprimirProva(questoesProva))
                        JOptionPane.showMessageDialog(null, "Prova imprimida com sucesso!", "Arquivo Gerado!", JOptionPane.INFORMATION_MESSAGE, MediaController.createImageIcon("Imagens/Success.png"));
                }
        }
        catch(Exception ex)
        {
            JOptionPane.showMessageDialog(null, ex.getMessage(), "Erro!", JOptionPane.ERROR_MESSAGE, MediaController.createImageIcon("Imagens/Error.png"));
        }
    }//GEN-LAST:event_btnImprimriProvaActionPerformed

    private void lbxQuestoesMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lbxQuestoesMouseClicked

        boolean temItemSelecionado = !this.lbxQuestoes.isSelectionEmpty();

        if(temItemSelecionado)
        {
            QuestaoProva questaoSelecionada = (QuestaoProva)this.lbxQuestoes.getSelectedValue();

            TelaQuestaoProva telaQuestaoProva = new TelaQuestaoProva(questaoSelecionada, questoesProva.size());
            telaQuestaoProva.setVisible(true);
        }
    }//GEN-LAST:event_lbxQuestoesMouseClicked

    private void btnImprimirGabaritoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnImprimirGabaritoActionPerformed

       Prova p = new Prova();
       p.setQuestoesProva(questoesProva);
       
       try
       {
           if(p.validaPontuacaoTotalProva(questoesProva))
               if(p.verificaPosicaoQuestoesProva(questoesProva))
               {
                   HTMLGenerator html = new HTMLGenerator();
                   if (html.imprimirGabarito(questoesProva))
                        JOptionPane.showMessageDialog(null, "Gabarito imprimido com sucesso!", "Arquivo Gerado!", JOptionPane.INFORMATION_MESSAGE, MediaController.createImageIcon("Imagens/Success.png"));
               }
        }
        catch(Exception ex)
        {
            MediaController.showErrorMessage(ex.getMessage(), "Erro!");
        }
    }//GEN-LAST:event_btnImprimirGabaritoActionPerformed

    private void populaListaQuestoesProva()
    {
        this.lbxQuestoes.setListData(questoesProva.toArray());
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnImprimirGabarito;
    private javax.swing.JButton btnImprimriProva;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lbProva;
    private javax.swing.JLabel lbQuestoes;
    private javax.swing.JList lbxQuestoes;
    // End of variables declaration//GEN-END:variables

}
